﻿using System;
using fastCSharp;

namespace diantou.web.contest
{
    /// <summary>
    /// 相关收藏
    /// </summary>
    [fastCSharp.code.cSharp.webView(IsPool = true, IsReferer = false)]
    partial class favorite : view<favorite>
    {
        /// <summary>
        /// 竞赛相关收藏客户端缓存
        /// </summary>
        internal static readonly fastCSharp.sql.logCache<int, keyValue<int[], int>>.array Cache = new fastCSharp.sql.logCache<int, keyValue<int[], int>>.array(diantou.dataAccess.tcpCall.favorites.onContestLog, 4);
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="values"></param>
        /// <param name="cacheValue"></param>
        internal static void SetCache(diantou.dataAccess.favorite[] values, ref fastCSharp.sql.logCache<int, keyValue<int[], int>>.array.value cacheValue)
        {
            if (cacheValue.Identity != 0)
            {
                bool isCache = true;
                foreach (diantou.dataAccess.favorite favorite in values)
                {
                    if (favorite.Privacy != dataModel.privacy.AllUser)
                    {
                        isCache = false;
                        break;
                    }
                }
                if (isCache) Cache.Set(ref cacheValue);
            }
        }
        /// <summary>
        /// 收藏集合
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        private diantou.web.favorite.userFavorite[] favorites;
        /// <summary>
        /// 相关收藏
        /// </summary>
        /// <param name="contestId">比赛标识</param>
        /// <param name="page">分页号</param>
        /// <returns>是否成功</returns>
        private bool loadView(int contestId, string randomCode, int page)
        {
            if (base.loadView(contestId, randomCode))
            {
                diantou.dataAccess.favorite[] values;
                fastCSharp.sql.logCache<int, keyValue<int[], int>>.array.value cacheValue = page <= 1 ? Cache.Get(contestId) : default(fastCSharp.sql.logCache<int, keyValue<int[], int>>.array.value);
                if (cacheValue.Value.Key == null)
                {
                    cacheValue.Value.Key = diantou.dataAccess.tcpCall.favorite.relateIds(dataModel.favorite.type.Contest, contestId, fastCSharp.config.pub.Default.PageSize, page, currentUserId, out cacheValue.Value.Value);
                }
                values = diantou.dataAccess.clientCache.Favorite.Get(cacheValue.Value.Key);
                SetCache(values, ref cacheValue);
                pageInfo.Page = page;
                pageInfo.Set(fastCSharp.config.pub.Default.PageSize, cacheValue.Value.Value);
                favorites = values.getArray(value => new diantou.web.favorite.userFavorite { Favorite = value, UserId = currentUserId });
                if (requestHeader.IsSearchEngine) pageInfo.Path = contest.Path.Favorite.ToPageString();
                return true;
            }
            return false;
        }
    }
}
